package cn.zhc.com.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.zhc.com.event.EventInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zhouhongchun on 2017/8/9.
 */

public abstract class BaseFragment extends Fragment {
    /**
     * Attach 的 Activity
     */

    protected Activity mActivity;

    /**
     * 根View
     */
    protected View viewRoot;


    private Unbinder unbinder;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 获得全局的，防止使用getActivity()为空
     * @param context
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = getActivity();
    }
    public Activity getmActivity(){
        return mActivity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.viewRoot = inflater.inflate(bindLayout(), container, false);
        isInit = true;

        return viewRoot;
    }
    private boolean hidden = false;
    /**
     * fragment被影藏是返回true,show时返回false
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        this.hidden = hidden;
        if (!hidden) {
//            refresh();
        }
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, viewRoot);
        EventBus.getDefault().register(this);
        init(getArguments());
        setAllListener();
        isCanLoadData();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isInit = false;
        isLoad = false;
        if (unbinder != null){
            unbinder.unbind();
        }
        EventBus.getDefault().unregister(this);

    }

    /**
     * 与ViewPager结合使用时使用，ViewPager默认有个预加载
     * 用户可见时执行的操作,懒加载操作
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanLoadData();

    }
    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected boolean isLoad = false;
    /**
     * 是否可以加载数据
     * 可以加载数据的条件：
     * 1.视图已经初始化
     * 2.视图对用户可见
     */
    private void isCanLoadData() {
        if (!isInit) {

            return;
        }

        if (getUserVisibleHint()&&!isLoad) {
            onLazyLoad();
            isLoad = true;
        } else {
            if (isLoad) {
                onStopLoad();
            }
        }
    }


    /**
     * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以覆写此方法
     */
    protected void onStopLoad() {
    }

    /**
     * 懒加载，
     * 当视图初始化并且对用户可见的时候去真正的加载数据
     */
    protected void onLazyLoad() {

    }

    /**
     * @return 设置根布局资源id
     */
    public abstract int bindLayout();



    /**
     * 设置所有的事件监听
     */
    protected void setAllListener() {

    }

    /**
     * 初始化数据
     *
     * @param arguments 接收到的从其他地方传递过来的参数
     */
    protected abstract void init(Bundle arguments);
    protected abstract void onEventBusMainThread(EventInfo mEventInfo);
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventInfo mEventInfo) {
        onEventBusMainThread(mEventInfo);
    }
}
